<?php

class Lw_log
{
    protected $CI;
    private $logDate;

    public function __construct()
    {
        $this->CI =& get_instance();
        $this->CI->load->database();
        $this->logDate = date('Y-m-d H:m:s', time());
    }

    public function systemLog($segments,$param)
    {
        $logInfo = array();
        $logInfo['controller'] = isset($segments[1])? $segments[1] : "默认主页";
        $logInfo['method'] = isset($segments[2])? $segments[2] : null;
        $logInfo['get'] = isset($segments[3])? $segments[3] : null;
        $logInfo['post'] = isset($param)? json_encode($param) : null;
        $logInfo['ip'] = $this->getIP();
        $logInfo['record_datetime'] = $this->logDate;
        $this->CI->db->insert("tb_sys_log",$logInfo);
    }

    public function getIP()
    {
        if (getenv("HTTP_CLIENT_IP")) {
            $IP = getenv("HTTP_CLIENT_IP");
        } else if(getenv("HTTP_X_FORWARDED_FOR")) {
            $IP = getenv("HTTP_X_FORWARDED_FOR");
        } else if(getenv("REMOTE_ADDR")){
            $IP = getenv("REMOTE_ADDR");
        } else{
            $IP = "Unknow";
        }
        return $IP;
    }
    
    
}